package com.ss.android.newmedia.feedback;

import android.content.Context;
import android.os.Build;
import android.os.Message;
import android.text.TextUtils;
import com.bytedance.article.common.c.TTUtils;
import com.bytedance.article.common.c.VideoLogManager;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.b.ThreadPlus;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.frameworks.baselib.network.http.parser.a;
import com.bytedance.retrofit2.Call;
import com.bytedance.retrofit2.mime.TypedFile;
import com.bytedance.retrofit2.mime.TypedString;
import com.bytedance.ttnet.utils.RetrofitUtils;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.app.AbsApplication;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.util.NetworkUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;

public final class j extends ThreadPlus {
    a[] a;
    boolean b;
    private final WeakHandler c;
    private final Context d;
    private f e;
    private n f;
    private m g;
    private boolean h;

    public j(WeakHandler arg3, Context arg4, n arg5) {

        this.a = new a[1];
        this.b = false;
        this.h = true;
        this.c = arg3;
        this.d = arg4.getApplicationContext();
        this.f = arg5;
    }

    public j(WeakHandler arg4, Context arg5, m arg6) {

        this.a = new a[1];
        this.b = false;
        this.h = true;
        this.c = arg4;
        this.d = arg5.getApplicationContext();
        this.g = arg6;
        this.h = false;
    }

    private int a() {
        return this.a(0, this.e.a(true), false);
    }

    private int a(long arg12, long arg14, boolean arg16) {
        int v4;
        ArrayList v8 = new ArrayList();
        ArrayList v9 = new ArrayList();
        long v6 = -1;
        long v2 = arg12;
        while(true) {
            n v0 = new n(this.f.d, v2, arg14);
            v4 = this.a(((List)v8), ((List)v9), v0);
            if(v4 == 0 && ((List)v8).size() > 0) {
                v2 = (( com.ss.android.newmedia.feedback.g)((List)v8).get(((List)v8).size() - 1)).a;
                if(v6 != v2 && arg14 < v2 && (v0.g)) {
                    v6 = v2;
                    continue;
                }
            }

            break;
        }

        if((arg16) && v4 == 0) {
            this.e.a();
        }

        this.e.a(((List)v8));
        this.e.a(((List)v9));
        return v4;
    }

    private int a(List arg13, List arg14, n arg15) {
        boolean v0_3;
        int v0_2;
        int v1 = 18;
        long v10 = 0;
        try {
            LinkedHashMap v3 = new LinkedHashMap();
            ((Map)v3).put("appkey", arg15.d);
            if(arg15.c > 0) {
                ((Map)v3).put("count", String.valueOf(arg15.c));
            }

            if(arg15.b > v10) {
                ((Map)v3).put("min_id", String.valueOf(arg15.b));
            }

            if(arg15.a > v10) {
                ((Map)v3).put("max_id", String.valueOf(arg15.a));
            }

            Object v0_1 = RetrofitUtils.createService("http://ichannel.snssdk.com", IFeedbackApi.class).feedbackList(307200, v3).execute().body;
            if(android.support.design.a.isTextEmpty(((String)v0_1))) {
                v0_2 = v1;
                return ((int)v0_2);
            }

            JSONObject v3_1 = new JSONObject(((String)v0_1));
            if(!"success".equals(v3_1.optString("message"))) {
                if("session_expired".equals(v3_1.optJSONObject("data").optString("name"))) {
                    v0_2 = 105;
                    return ((int)v0_2);
                }

                v0_2 = v1;
                return ((int)v0_2);
            }

            JSONArray v1_1 = v3_1.optJSONArray("data");
            int v4 = v1_1.length();
            if(v3_1.isNull("has_more")) {
                v0_3 = v4 >= arg15.c ? true : false;
                arg15.g = v0_3;
            } else {
                arg15.g = v3_1.optBoolean("has_more", false);
            }

            for(v0_2 = 0; v0_2 < v4; ++v0_2) {
                JSONObject v5 = v1_1.optJSONObject(v0_2);
                long v6 = v5.optLong("id", -1);
                if(v6 > v10) {
                    g v8 = new g(v6);
                    v8.a(v5);
                    arg13.add(v8);
                }
            }

            JSONObject v0_4 = v3_1.optJSONObject("default_item");
            if(v0_4 != null) {
                g v1_2 = new g(v0_4.optLong("id", 0));
                v1_2.a(v0_4);
                v1_2.h = 2;
                arg14.add(v1_2);
            }
        }
        catch(Throwable v0) {
            label_92:
            Logger.e("FeedbackThread2", "get feedback list error:" + v0);
            v0_2 = TTUtils.getErrorCode(this.d, v0);
            return ((int)v0_2);
        }

        v0_2 = 0;
        return ((int)v0_2);

    }

    public final void run() {
        LinkedHashMap v1_1 = null;
        String v0_5;
        Object v0_4;
        String v1 = null;
        Message v0_1;
        int v9 = 11;
        int v11 = -1;
        int v6 = 0;
        String v10 = null;
        long v4 = 0;
        this.e = com.ss.android.newmedia.feedback.f.a(this.d);
        int v2 = 0;
        try {
            if (this.h) {
                int v0 = 18;
                if (this.f.f == 2) {
                    v6 = this.a();
                    this.f.h = this.e.a(this.f.b, this.f.c, " DESC");
                } else if (this.f.f == 3) {
                    v6 = this.a();
                    this.f.h = this.e.a(this.f.b, v11, " DESC");
                    this.f.i = this.e.b();
                } else if (this.f.f == 5) {
                    this.f.h = this.e.a(v4, v11, v10);
                    this.f.i = this.e.b();
                } else {
                    if (this.f.f == 4) {
                        v0 = this.a();
                        if (v0 == 0) {
                            v6 = this.a(this.e.a(false), v4, false);
                            this.f.h = this.e.a(v4, v11, v10);
                            this.f.i = this.e.b();
                        } else {
                            v6 = v0;
                        }
                    } else if (this.f.f == 1) {
                        v6 = this.a(v4, v4, true);
                        this.f.h = this.e.a(v4, v11, v10);
                        this.f.i = this.e.b();
                    } else {
                        v6 = v0;
                    }
                }
                v0 = v6 == 0 ? 10 : v9;
                v0_1 = this.c.obtainMessage(v0);
                v0_1.obj = this.f;
                v0_1.arg1 = v6;
                this.c.sendMessage(v0_1);
                return;
            }

            v2 = 18;
            try {
                v1 = "";
                if (this.g.d == null) {
                    this.g.d = "";
                }

                if (!new File(this.g.d).exists()) {
                    v0_5 = v1;
                } else {
                    LinkedHashMap v3 = new LinkedHashMap();
                    ((Map) v3).put("watermark", new TypedString("0"));
                    ((Map) v3).put("image", new TypedFile(new File(this.g.d)));
                    Call v0_3 = RetrofitUtils.createService("http://isub.snssdk.com", IFeedbackApi.class).uploadImage(20480, ((Map) v3));
                    if (this.a != null && this.a.length > 0) {
                        this.a[0] = new a(v0_3);
                    }

                    v0_4 = v0_3.execute().body;
                    if (android.support.design.a.isTextEmpty(((String) v0_4))) {
                        return;
                    }

                    JSONObject v3_1 = new JSONObject(((String) v0_4));
                    if (!AbsApiThread.isApiSuccess(v3_1)) {
                        v2 = 16;
                        return;
                    }
                    v0_5 = v3_1.optJSONObject("data").optString("web_uri", "");
                }
            } catch (Throwable v0_2) {
                v0_2.printStackTrace();
                v0_5 = v1;
            }

            try {
                v1_1 = new LinkedHashMap();
                ((Map) v1_1).put("appkey", new TypedString(this.g.b));
                ((Map) v1_1).put("content", new TypedString(this.g.a));
                if (!android.support.design.a.isTextEmpty(this.g.c)) {
                    ((Map) v1_1).put("contact", new TypedString(this.g.c));
                }

                if (!android.support.design.a.isTextEmpty((String) null)) {
                    ((Map) v1_1).put("report_type", new TypedString(null));
                }

                if (!android.support.design.a.isTextEmpty((String) null)) {
                    ((Map) v1_1).put("report_content", new TypedString(null));
                }

                if (v4 > v4) {
                    ((Map) v1_1).put("user_id", new TypedString("0"));
                }

                if (!android.support.design.a.isTextEmpty(v0_5)) {
                    ((Map) v1_1).put("image_uri", new TypedString(v0_5));
                }

                ((Map) v1_1).put("device", new TypedString(Build.MODEL));
                try {
                    ((Map) v1_1).put("app_version", new TypedString(AbsApplication.getApplication().getAppVersion()));
                    ((Map) v1_1).put("sig_hash", new TypedString(AppLog.g(this.d)));
                } catch (Exception v0_6) {
                }
                v0_5 = VideoLogManager.getInstance().b();
                if (!TextUtils.isEmpty(((CharSequence) v0_5))) {
                    ((Map) v1_1).put("video_log", new TypedString(v0_5));
                }

                v0_5 = NetworkUtils.getNetworkAccessType(this.d);
                if (v0_5 != null) {
                    ((Map) v1_1).put("network_type", new TypedString(v0_5));
                }

                AppLog.j();
                com.ss.android.common.util.g.a();
                com.ss.android.common.util.g.a();
            } catch (Throwable v0_2) {
                v0_2.printStackTrace();
            }

            try {
                label_255:
                v0_4 = RetrofitUtils.createService("http://ichannel.snssdk.com", IFeedbackApi.class).postMessage(12040, ((Map) v1_1)).execute().body;
                if (!android.support.design.a.isTextEmpty(((String) v0_4))) {
                    JSONObject v1_2 = new JSONObject(((String) v0_4));
                    if ("success".equals(v1_2.optString("message"))) {
                        label_301:
                        v9 = 10;
                        v2 = v6;
                    } else if ("session_expired".equals(v1_2.optJSONObject("data").optString("name"))) {
                        v2 = 105;
                    } else {
                        v6 = v2;
                        v2 = v6;
                    }
                } else {
                    v6 = v2;
                    v2 = v6;
                }
            } catch (Throwable v0_2) {
                v2 = TTUtils.getErrorCode(this.d, v0_2);
                Logger.d("FeedbackThread2", "sumbit feedback error=" + v0_2);
            }
        }catch (Exception e) {

        }finally {
            if(!this.b) {
                v0_1 = this.c.obtainMessage(v9);
                v0_1.arg1 = v2;
                this.c.sendMessage(v0_1);
            }
        }
    }
}

